//
//  MainViewController.m
//  ZhangJiangJun
//
//  Created by FENGYL on 2017/1/13.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "MainViewController.h"
#import "StampLockViewController.h"
#import "DeviceManagerViewController.h"
#import "NetworkCommand.h"
#import "UserModel.h"

#import <FTPopOverMenu.h>
#import <MBProgressHUD.h>

@interface MainViewController ()

@property (weak, nonatomic) IBOutlet UIButton *stampButton;
@property (weak, nonatomic) IBOutlet UIButton *monitoringButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *menuManagerBarButtonItem;

@property (strong, nonatomic) UserModel *userModel;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
}

-(void)setUpUI{
    
    //UserModel
    self.userModel = [[UserModel alloc]init];
    
    //背景
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"zjj_j_d"]]];
    
    //按钮文字
    [self.stampButton setTitle:NSLocalizedString(LOCALIZABLE_STAMP, nil) forState:UIControlStateNormal];
    [self.monitoringButton setTitle:NSLocalizedString(LOCALIZABLE_MONITORING, nil) forState:UIControlStateNormal];
    
    //获取设备列表
    [self firstGetDeviceList];
    
    
}

#pragma mark 第一次获取设备列表
-(void)firstGetDeviceList{
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [hud.label setText:NSLocalizedString(LOCALIZABLE_LOADING, nil)];
    
    hud.mode = MBProgressHUDModeIndeterminate;
    
    hud.removeFromSuperViewOnHide = YES;
    
    [NetworkCommand getDeviceListSuccess:^{
        
        NSDictionary * dict = self.userModel.deviceByAccountArray[0];
        
        self.userModel.currentDeviceSerialNumber = [dict objectForKey:NC_SERIAL_NUMBER];
        
        self.userModel.currentDeviceName = [dict objectForKey:NC_DISPLAY_NAME];
        
        [hud.label setText:NSLocalizedString(LOCALIZABLE_GET_LIST_SUCCESS, nil)];
        
        hud.mode = MBProgressHUDModeCustomView;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
        
    } failure:^{
        
        [hud.label setText:NSLocalizedString(LOCALIZABLE_REFRESHING_FAILED, nil)];
        
        hud.mode = MBProgressHUDModeCustomView;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
    }];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    //透明nav
    UIImage * image = [[UIImage alloc]init];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:12/255.0 green:97/255.0 blue:255/255.0 alpha:1]];
    
}

-(void)viewWillDisappear:(BOOL)animated{
    
    //蓝色nav
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:64/255.0 green:103/255.0 blue:202/255.0 alpha:1]];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
}

#pragma mark 菜单按钮
- (IBAction)touchMenuManagerButton:(id)sender {
    
    [FTPopOverMenu showFromSenderFrame:CGRectMake(self.view.frame.size.width - 52, 20, 40, 40) withMenu:@[NSLocalizedString(LOCALIZABLE_STAMP_MANAGER, buk)] doneBlock:^(NSInteger selectedIndex) {
        
        UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        
//        if (selectedIndex == 0) {
//            
//            DeviceManagerViewController * deviceManagerViewController =      [storyboard instantiateViewControllerWithIdentifier:@"DeviceManagerViewController"];
//            
//            [self showViewController:deviceManagerViewController sender:nil];
//            
//        }else{
//            
//            
//            
//            StampLockViewController * stampLockViewController = [storyboard instantiateViewControllerWithIdentifier:@"StampLockViewController"];
//            
//            [self showViewController:stampLockViewController sender:nil];
//            
//        }
        
        DeviceManagerViewController * deviceManagerViewController =      [storyboard instantiateViewControllerWithIdentifier:@"DeviceManagerViewController"];
        
        [self showViewController:deviceManagerViewController sender:nil];
        
    } dismissBlock:^{
        
    }];
    
}

#pragma mark 登出按钮
- (IBAction)accountLogout:(id)sender {
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
